package com.peas.platform.module.icims.base.mdm.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.mdm.adapter.repository.GovernmentRepository;
import com.peas.platform.module.icims.base.mdm.adapter.repository.po.GovernmentPo;
import com.peas.platform.module.icims.base.mdm.domain.GovernmentDomain;
import com.peas.platform.module.icims.base.mdm.dto.GovernmentDto;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class GovernmentDomainImpl extends BaseDomainImpl<GovernmentDto, GovernmentPo> implements GovernmentDomain {

    @Resource
    private GovernmentRepository governmentRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<GovernmentPo> getMapper() {
        return governmentRepository;
    }

    @Override
    public Class<? extends BaseRepository<GovernmentPo>> getRepositoryClass() {
        return GovernmentRepository.class;
    }

    @Transactional
    @Override
    public int save(GovernmentDto dto) {
        if (StrUtil.isBlank(dto.getGovernmentCode())) {
            String code = sequenceService.genSequence("base", "GovernmentDto", "GD", 4, 1);
            dto.setGovernmentCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("governmentCode", dto.getGovernmentCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(GovernmentDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("governmentCode", dto.getGovernmentCode());
        return result;
    }

}
